package com.llc.mall.user.interfaces.app;

import com.llc.mall.common.core.response.ServerResponseEntity;
import com.llc.mall.common.security.AuthUserContext;
import com.llc.mall.user.application.command.service.UserAddrAppService;
import com.llc.mall.user.application.query.dto.UserAddrDTO;
import com.llc.mall.user.application.query.service.UserAddrQueryService;
import com.llc.mall.user.interfaces.vo.UserAddrVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

@Tag(name = "app-用户地址")
@RequestMapping("/user_addr")
@RestController
public class UserAddrController {

    @Resource
    private UserAddrAppService userAddrAppService;

    @Resource
    private UserAddrQueryService userAddrQueryService;

    @Operation(summary = "新增用户地址" , description = "保存用户地址")
    @PostMapping
    public ServerResponseEntity<Void> save(@Valid @RequestBody UserAddrDTO userAddrDTO) {
        userAddrAppService.add(userAddrDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新用户地址" , description = "更新用户地址")
    public ServerResponseEntity<Void> update(@Valid @RequestBody UserAddrDTO userAddrDTO) {
        userAddrAppService.modify(userAddrDTO);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除用户地址" , description = "根据用户地址id删除用户地址")
    public ServerResponseEntity<Void> delete(@RequestParam Long addrId) {
        userAddrAppService.delete(addrId);
        return ServerResponseEntity.success();
    }


    @GetMapping("/list")
    @Operation(summary = "获取用户地址列表" , description = "获取用户地址列表")
    public ServerResponseEntity<List<UserAddrVo>> list() {
        Long userId = AuthUserContext.get().getUserId();
        List<UserAddrVo> userAddrPage = userAddrQueryService.list(userId);
        return ServerResponseEntity.success(userAddrPage);
    }

    @GetMapping
    @Operation(summary = "获取用户地址" , description = "根据addrId获取用户地址")
    public ServerResponseEntity<UserAddrVo> getByAddrId(@RequestParam Long addrId) {
        return ServerResponseEntity.success(userAddrQueryService.getUserAddrByUserId(addrId,AuthUserContext.get().getUserId()));
    }

}
